<?php
namespace ZHMVC\DB\MANAGER;
if (! isset($_SESSION)) {
    session_start();
}
include (dirname(dirname(__FILE__)) . "/zhconfig/Config.php");
include (ZH_PATH . DS . MANAGERFPATH . DS . 'islogin' . ZH);

$isp = new \ZHMVC\D\MANAGER\isPermission();
$isper = $isp->getPermission();
$_curlid = $isp->getCUrl();
$c = new \ZHCONFIG\ZhConfig();
$db_pre = $c->getDbPre();

if($isper==1)
{
    $ErrMsg="对不起，你没有访问该页面的权限";
    echo $ErrMsg;
    exit;
}
elseif($isper==0)
{
    $ErrMsg="对不起，地址错误";
    echo $ErrMsg;
    exit;
}

include (ZH_PATH . DS . MANAGERFPATH . DS . 'top1' . ZH);
?>
<div class="wap-container">
	<nav class="breadcrumb" style="background-color:#fff;padding: 0 24px">
	管理员管理
	<a class="btn btn-success radius f-r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a>
	</nav>
<?php 
$action=SafeRequest(getPGC('atcion'),0);
switch ($action) {
    case "save":
        save($db_pre);
        break;
    case "add":
        add($db_pre);
        break;
    case "del":
        del($db_pre);
        break;
    default:
        main($db_pre);
}

function main($db_pre)
{
    ?>
<article class="Hui-admin-content clearfix">
<div class="panel mt-20">
	<div class="panel-body">
		<div class="clearfix">
			<span class="f-l">
				<a href="javascript:;" onClick="location='?atcion=add';" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 添加</a>
			</span>
		</div>
		<div class="clearfix mt-20">
						<table class="table table-striped table-bordered">
							<thead>
								<tr>
									<th>Id</th>
									<th>类别</th>
									<th>序号</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody>
  <?php
    $rs1 = D("" . $db_pre . "optionsclass")->getLinkAll("id", true);
    $datas1 = $rs1['datas'];
    $rows1 = $rs1['rows'];
    If ($rows1 <= 0) {
        $OutStr = "<tr>";
        $OutStr = $OutStr . "<td colspan=10>&nbsp;<font color=\"red\">暂无内容</font></td>";
        $OutStr = $OutStr . "</tr></tbody>";
        echo $OutStr;
    } else {
        $pa = new \ZHMVC\B\TOOL\ShowPages();
        $pa->pvar = "pg";
        $pa->set(20, $rows1);
        $rs = D("" . $db_pre . "optionsclass")->order("id desc")
        ->limit($pa->limit())
        ->getLinkAll("*", true);
        $datas = $rs['datas'];
        $rows = $rs['rows'];
        // print_r($datas);
        for ($i = 0; $i < $rows; $i ++) {
            $data = $datas[$i];
            ?>
  <tr>
									<td><?php echo $data['id']; ?></td>
									<td><?php echo $data['title']; ?></td>
									<td><?php echo $data['paixu']; ?></td>
									<td class="taskOptions"><a
										href="?atcion=add&postid=<?php echo $data['id']; ?>">编辑</a> |
										<a href="?atcion=del&postid=<?php echo $data['id']; ?>"
										onclick="{if(confirm('确定删除吗?')){return true;}return false;}">删除</a></td>
								</tr>
<?php
        }
        ?>
    </tbody>
							<tfoot>
								<tr>
									<td colspan="10">&nbsp;<?php $pa -> output(0); ?></td>
								</tr>
							</tfoot>
<?php
    }
    ?>
</table>					</div>
				</div>
			</div>
</article>
<?php
}

function add($db_pre)
{
    $postid = SafeRequest(getPGC("postid"), 0);
    
    if (($postid != "") && ($postid != "0")) {
        $bind = array(
            "id" => $postid
        );
        $rs = D("" . $db_pre . "optionsclass")->where($bind)->getLinkOne("*", true);
        $data = $rs['datas'];
        $rows = $rs['rows'];
        If ($rows != 0) {
            $title = $data['title'];
            $description = $data['description'];
            $bgcolor = $data['bgcolor'];
            $isok = $data['isok'];
            $paixu = $data['paixu'];
        } else {
            $title = '';
            $description = '';
            $bgcolor = '#ffffff';
            $isok = '0';
            $paixu = 0;
        }
    } else {
        $title = '';
        $description = '';
            $bgcolor = '';
            $isok = '0';
            $paixu = 0;
    }
    ?>
<div class="panel">
	<div class="panel-body">
<form action="?atcion=save&postid=<?php echo $postid; ?>" id="form1"
		name="form1" method="post" class="form form-horizontal">
									<!-- 表单 -->
		<div class="row clearfix">
			<label class="form-label col-xs-4 col-sm-3">名称：</label>
			<div class="form-controls col-xs-8 col-sm-9">
				<input type="text" name="title" class="input-text" placeholder=""
							value="<?php echo $title; ?>" />
			</div>
		</div>
		
		<div class="row clearfix">
			<label class="form-label col-xs-4 col-sm-3">描述：</label>
			<div class="form-controls col-xs-8 col-sm-9">
				<input type="text" name="description" class="input-text" placeholder=""
							value="<?php echo $description; ?>" />
			</div>
		</div>
		
		<div class="row clearfix">
			<label class="form-label col-xs-4 col-sm-3">背景颜色：</label>
			<div class="form-controls col-xs-8 col-sm-9">
				<input type="text" name="bgcolor" class="input-text" placeholder=""
							value="<?php echo $bgcolor; ?>" />
			</div>
		</div>
			
		<div class="row clearfix">
			<label class="form-label col-xs-4 col-sm-3">是否启用：</label>
			<div class="form-controls col-xs-8 col-sm-9">
				<div class="radio-box">
					<input name="isok" type="radio" value="1" id="isok-1"  <?php
					if ($isok == "1") { echo "checked";}?> />
							<label for="isok-1">是</label>
						</div>
						<div class="radio-box">
							<input type="radio" id="isok-2" value="0" name="isok"
							<?php
					if ($isok == "0") { echo "checked";}?> />
							<label for="isok-2">否</label>
						</div>
					</div>
			</div>
			
		<div class="row clearfix">
			<label class="form-label col-xs-4 col-sm-3">序号：</label>
			<div class="form-controls col-xs-8 col-sm-9">
				<input type="text" name="paixu" class="input-text" placeholder=""
							value="<?php echo $paixu; ?>" /><br>
							序号越大越靠前，倒序
			</div>
		</div>
		
		

									
				 <div class="row clearfix">
					<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
						<input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
					</div>
				</div>
				</form>
				<!-- 表单 -->
          </div>
    	</div>
<?php
}

function save($db_pre)
{
    $postid = SafeRequest(getPGC("postid"), 0);
    $title = SafeRequest(getPGC("title"), 0);
    $description = SafeRequest(getPGC("description"), 0);
    $bgcolor= SafeRequest(getPGC("bgcolor"), 0);
    $isok= SafeRequest(getPGC("isok"), 0);
    $paixu= SafeRequest(getPGC("paixu"), 0);
    
    If ($title == "") {
        $ErrMsg = "对不起，长度不符！";
        $ErrMsg = $ErrMsg . "<li>长度等于0或大于250";
        echo $ErrMsg;
        exit();
    }
    
    if (($postid != "") && ($postid != "0")) {
        $updatedata = array(
            "title" => $title,
            "description" => $description,
            "bgcolor" => $bgcolor,
            "isok" => $isok,
            "paixu" => $paixu
        );
        $wheremap = array(
            "id" => $postid
        );
        D("" . $db_pre . "optionsclass")->where($wheremap)->LinkUpdate($updatedata);
    } else {
        $IdWorker = \wantp\Snowflake\IdWorker::getIns();
        $id = $IdWorker->id();
        $updatedata = array(
            "id" => $id,
            "title" => $title,
            "description" => $description,
            "bgcolor" => $bgcolor,
            "isok" => $isok,
            "paixu" => $paixu
        );
        D("" . $db_pre . "optionsclass")->LinkInsert($updatedata);
    }
    echo "<script>alert('更新成功');window.location.href='admin_optionsclass.php';</script>";
}

function del($db_pre)
{
    $postid = SafeRequest(getPGC("postid"), 0);
    if (($postid != "") && ($postid != "0")) {
        $wheremap = array(
            "id" => $postid
        );
        D("" . $db_pre . "optionsclass")->where($wheremap)->LinkDelete($wheremap);
        echo "<script>alert('更新成功');window.location.href='admin_optionsclass.php';</script>";
    }
}
?>
</div>
<?php 
include (ZH_PATH . DS . MANAGERFPATH . DS . 'foot1' . ZH);
?>
	<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>
